'''

Unit info
Implement listctrl-class date with from database.
Listctrl:
- a row exists of items
- the first item holds the ID of the row
- the other items of the row are hung onto this ID

wxPython note from help-file
    In place of a single overloaded method name, wxPython implements the following methods:
        SetItem(item)  Sets information about the given wxListItem.
        SetStringItem(index, col, label, imageId)  Sets a string or image at a given location.

ADAP HIS
-remove setdatabase and migrate to pardb.py

ADAP NO


ADAP FUT
-check if a virtual list-control is usefull (see wxwidgets) vrs_3.0
'''

#!/usr/bin/python

# actresses.py

import wx
import sys
##import jodb
import pardb
import jotools
import db_species

packages = [('jessica alba', 'pomona', '1981'), ('sigourney weaver', 'new york', '1949'),
    ('angelina jolie', 'los angeles', '1975'), ('natalie portman', 'jerusalem', '1981'),
    ('rachel weiss', 'london', '1971'), ('scarlett johansson', 'new york', '1984' )]


class listPanel(wx.Panel):
    def __init__(self, parent, ID, pos = wx.DefaultPosition,
            size = wx.DefaultSize):
        wx.Panel.__init__(self, parent,ID, pos, size)
        #--------------------------------------------------
        self.hbox = wx.BoxSizer(wx.HORIZONTAL)
        # for now style is set to single selection.
        self.lisc = wx.ListCtrl(self, -1, style=wx.LC_REPORT + wx.LC_SINGLE_SEL )
        self.hbox.Add(self.lisc, 1, wx.EXPAND)
        self.SetSizer(self.hbox)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onAnyEvent, self.lisc)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.onAnyEvent, self.lisc)
##        print '****listcontrol-functions****'
##        jotools.prList(dir(self.lisc))

    def onAnyEvent(self, evt):
        '''
        # override this one on call
        '''

        print evt.GetEventType()
        if evt.GetEventType() == int(wx.EVT_LIST_ITEM_SELECTED._getEvtType()):
            print 'selected'
        elif evt.GetEventType() == int(wx.EVT_LIST_ITEM_ACTIVATED._getEvtType()):
            print 'activated'

##    def setDatabase(self, databasest):
##        self.db = databasest
##        db_species.curDatabase = self.db

    def setColumns(self,defLiscli):
        self.lisc.ClearAll()    # clear both columns and items
        for col in defLiscli:
            self.lisc.InsertColumn(col[1], col[0], width=col[2])

    def setDataParams(self, *sqlParams):
        self.lisc.DeleteAllItems()
        curli = pardb.readFromParams(*sqlParams)
        self.writeListItems(curli)

    def setDataSql(self, dbst, sqlst):
        # still to be retested
        self.lisc.DeleteAllItems()
        curli = pardb.readFromSql(dbst, sqlst)
        self.writeListItems(curli)

##    def writeListItemsOld(self, cursorli):
##        for row in cursorli:
##            indexit = self.lisc.InsertStringItem(sys.maxint, str(row[0]))
##            n = 0
##            for val in row:
##                if n > 0:   # first item already asigned
##                    # ---------------
##                    # remove the 'nones'
##                    valst = str(val)
##                    if valst == 'None':
##                        valst = ''
##                    else:
##                        valst = str(val)
##                    # ---------------
##                    self.lisc.SetStringItem(indexit, n , valst)
##                n = n + 1


    def writeListItems(self, cursorli):
        for row in cursorli:
            indexit = self.lisc.InsertStringItem(sys.maxint, str(row[0]))
            n = 0
            for val in row:
                if n > 0:   # first item already asigned
                    if str(type(val)) == "<type 'NoneType'>":
                        val = ''
                    elif str(type(val)) == "<type 'unicode'>":
                        pass
                    else:
                        val = str(val)
                    # ---------------
                    self.lisc.SetStringItem(indexit, n , val)
                n = n + 1


    def getColText(self, colit):
        #get the value of the given column of the focused item
        idit = self.lisc.GetFocusedItem()
        item = self.lisc.GetItem(idit, colit)
        return item.GetText()

    def setColorByCondition(self, wxcolorit, columnit, valuest,
            substringbo = False):
        '''
        UNIT INFO:
        Set the wxcolorit of the row based on the valuest that columnit
        must have, optionally can contain as a substring.
        
        Sample-code from wx:
            # show how to change the colour of a couple items
            item = self.list.GetItem(1)
            item.SetTextColour(wx.BLUE)
            self.list.SetItem(item)

        ADAP FU

        '''

        rowcountit = self.lisc.GetItemCount()
##        print 'rowcount = ' + str(rowcountit)
##        print 'valuest = ' + valuest
        # thru-cycle the rows
        for rowit in range(0, rowcountit - 1):
            # (p)reset condition
            conditionbo = False
            # get the elements
            baseitem = self.lisc.GetItem(rowit)
            valitem = self.lisc.GetItem(rowit, columnit)
            # check specific condition
##            print baseitem.GetText()
##            print valitem.GetText()

            if substringbo == False:
                if valitem.GetText() == valuest:
                    conditionbo = True
            else:
                if valuest in valitem.GetText():
                    conditionbo = True

            if conditionbo:
                # set the color
                baseitem.SetTextColour(wxcolorit)
                self.lisc.SetItem(baseitem)


class someFrame (wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500, 400))

        lisp = listPanel(self, -1)
        defLiscli = [
                    ['RelID',0,5],
                    ['Subject',1,100],
                    ['Object1',2, 100],
                    ['Relation',3, 100],
                    ['Object2',4, 1],
                    ['Change',5,25],
                    ['Select',6,25],
                    ['Remark',7,75],
                    ['ModDate',8,50],
                    ['Source',9,75],
                    ['MsNumber',10,50],
                    ['MsDate',11,50],
                    ['MsCurrency',12,50]
                    ['MsLongText',13,50]
                    ]

        lisp.setColumns(defLiscli)
        lisp.setDataParams('', 'Relations', None, {'strings':{'Subject':'Kennis'}})
        lisp.setColorByCondition(wx.RED,4,'instantiering')
        lisp.lisc.Select(0,1)

##        item = lisp.lisc.GetItem(1)
##        item.SetTextColour(wx.BLUE)
##        lisp.lisc.SetItem(item)

class myApp(wx.App):
    def OnInit(self):
        frm = someFrame(None, -1, 'ListCtrl')
        frm.Show(True)
        self.SetTopWindow(frm)
        return True

if __name__ == '__main__':
    app = myApp()
    app.MainLoop()

